//
//  MainViewController.swift
//  WeiBoDemo
//
//  Created by mac on 2017/11/14.
//  Copyright © 2017年 mac. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    private lazy var imagesNames = ["icon_home","icon_rec","","icon_msg","icon_gro"]
    
    private lazy var composeBtn : UIButton = UIButton.init(imageName: "logo_log", bgImageName: "logo_log")
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupComposeBtn()

//        addChildViewController(childVC: MatchViewController(), title: "媒婆榜", imageName: "icon_home")
//        addChildViewController(childVC: DiscoverViewController(), title: "发现", imageName: "icon_rec")
//        addChildViewController(childVC: MsgViewController(), title: "消息", imageName: "icon_msg")
//        addChildViewController(childVC: MeViewController(), title: "我的", imageName: "icon_gro")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        setupTabBarItem()
    }
    
    //swift支持方法的重载
    //方法的重载：方法名称相同，但是参数不同， --> 1.参数的类型不同 2.参数的个数不同
    private func addChildViewController(childVC: UIViewController, title:String, imageName:String) {
        //1.设置子控制器的属性
        childVC.title = title
        childVC.view.backgroundColor = UIColor.gray
        childVC.tabBarItem.image = UIImage.init(named: imageName)
        childVC.tabBarItem.selectedImage = UIImage.init(named: imageName + "G")
        //2.包装导航控制器
        let nav = UINavigationController.init(rootViewController: childVC)
        //3.添加控制器
        addChildViewController(nav)
    }

   

}

//MARK: -设置UI界面
extension MainViewController {
    //设置发布按钮
    private func setupComposeBtn(){
        //1.将composeBtn添加到tabBar中
        tabBar.addSubview(composeBtn)
        
        //2.设置属性
//        composeBtn.setBackgroundImage(UIImage.init(named: "logo_log"), for: .normal)
//        composeBtn.setBackgroundImage(UIImage.init(named: "logo_log"), for: .highlighted)
//        composeBtn.setImage(UIImage.init(named: "logo_log"), for: .normal)
//        composeBtn.setImage(UIImage.init(named: "logo_log"), for: .highlighted)
//        composeBtn.sizeToFit()
        composeBtn.center = CGPoint.init(x: tabBar.center.x, y: tabBar.bounds.size.height * 0.5)
        
        
        
        //3.监听发布按钮的点击
        // Selector 两种写法： 1.Selector("方法名")  例如 Selector("composeBtnClick")
        //                   2.直接写方法名         例如 "composeBtnClick"
        composeBtn.addTarget(self, action: #selector(composeBtnClick), for: .touchUpInside)
    }
    
    private func setupTabBarItem(){
        //1.遍历所有的item
        for i in 0..<tabBar.items!.count{
            //2.获取item
            let item = tabBar.items![i]
            //3.如果是下标值为2，则该item不可以，交互
            if i == 2{
                item.isEnabled = false
                continue
            }
            //4.设备其它item的选中时候的图片
            item.selectedImage = UIImage.init(named: imagesNames[i])
        }
    }
    

    
}

//MARK: -事件监听
extension MainViewController {
    // 事件监听本质是发送消息，但是发送消息是OC的特性
    // 将方法包装成SEL --> 类中查找方法列表（方法列表中找不到私有方法，如果在方法前面加 @objc 关键词时，依然可以添加到方法列表中了） --> 根据@SEL找到imp指针（函数指针）--> 执行函数
    // 如果swift中将一个函数声明成private，那么该函数不会被指针添加到方法列表中
    @objc private func composeBtnClick(){
        print("发布")
    }
}

